package com.fruit.controller.app.sysmsg;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fruit.controller.base.BaseController;
import com.fruit.pojo.common.Page;
import com.fruit.service.sysmsg.SysMessageService;
import com.fruit.util.ResultInfo;

import lombok.extern.slf4j.Slf4j;

/**
 * 站内消息controller
 * 
 * @author zhenhw
 *
 */
@Slf4j
@Controller
@RequestMapping(value = "/app/sysmsg")
public class SysMsgController extends BaseController {
	@Autowired
	private SysMessageService sysMessageService;

	/**
	 * 获取站内消息
	 * 
	 * @param page
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/getMsgList", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> getMsgList(Integer userId, Page page, HttpServletRequest request, HttpSession session) {
		log.info("【获取系统消息列表】---【开始】---");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> pam = new HashMap<String, Object>();
		try {
			if (userId == null) {//用户标识
				resultMap.put("result", this.initResult(ResultInfo.ERROR, "param", "用户id不能为空"));
				return resultMap;
			}
			if (page == null) {//分页参数
				resultMap.put("result", this.initResult(ResultInfo.ERROR, "param", "分页参数必填"));
				return resultMap;
			}
			pam.put("start", (page.getPage() - 1) * page.getShowNum());
			pam.put("num", page.getShowNum());
			pam.put("userId", userId);
			ResultInfo res = sysMessageService.selectByUserId(pam);
			
			resultMap.put("msgList", res.getObj());
			resultMap.put("result", this.initResult(res.getState(), res.getCode(), res.getMessage()));
		} catch (Exception e) {
			log.info("获取系统消息列表---[异常]---{}", e);
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		log.info("【获取系统消息列表】---【结束】---");
		return resultMap;
	}
	/**
	 * 根据id标记消息为已读
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/updateState", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String,Object> updateState(String ids){
		log.info("【更新系统消息状态】---【开始】---id={}",ids);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			if (StringUtils.isEmpty(ids)) {
				resultMap.put("result", this.initResult(ResultInfo.ERROR, "param", "消息ids不能为空"));
				return resultMap;
			}
			ResultInfo res = sysMessageService.updateByIds(ids);
			resultMap.put("result", this.initResult(res.getState(), res.getCode(), res.getMessage()));
		} catch (Exception e) {
			log.info("【更新系统消息状态】---[异常]---{}", e);
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		log.info("【更新系统消息状态】---【结束】---");
		return resultMap;
	}
}
